#pragma once

#include "effectbase.h"
#include "commonshape.h"
#include "videoeffect.h"
#include <osg\NodeCallback>
#include <osg\Billboard>

namespace VrDataCore
{
	class BillboardVideoUpdateCallback;

	class VRDATACORE_EXPORT BillboardVideoEffectDrawable : public CoreBase::Drawable
	{
	public: 
		BillboardVideoEffectDrawable(CoreBase::ActorBase* owneractor);
		void reset(const osg::Vec3& p1,const osg::Vec3& p2,const osg::Vec3& p3,const osg::Vec3& p4,
			const osg::Vec2& t1,const osg::Vec2& t2,const osg::Vec2& t3,const osg::Vec2& t4,osg::StateSet* ss=0);
		osg::Node* getOSGNode();
		const osg::Node* getOSGNode() const;
		void setPosition(const osg::Vec3& pos);
	protected:
		virtual ~BillboardVideoEffectDrawable() {};
		osg::ref_ptr<osg::Billboard>				_billboardNode;
		osg::ref_ptr<CoreBase::PlaneShape>			_drawable;
	};

	class VRDATACORE_EXPORT BillboardVideoEffect:public EffectBase
	{
	public:
		BillboardVideoEffect();
	protected:
		~BillboardVideoEffect();
		virtual void createDrawable();
		osg::ref_ptr<VideoEffect>					_ve;
		QString										_videofile;
		osg::ref_ptr<BillboardVideoUpdateCallback>	_videoUpdateCallback;
		double										_videoLength;
		osg::Timer_t								_startT;
		bool										_loop;
		bool										_autounApply;
		bool										_setupCallback;
		osg::Vec3									_position;
	public:
		virtual void buildPropertyMap();
		virtual void apply(CoreBase::ActorBase* actor);
		virtual void unApply(CoreBase::ActorBase* actor);
		void setVideoFileName(QString f);
		QString getVideoFileName();
		void callback();
		void setLoop(bool loop);
		void setAutounApply(bool autounapply);
		void setPosition(const osg::Vec3& pos);
	};

	class BillboardVideoUpdateCallback : public osg::NodeCallback
	{
	public:
		BillboardVideoUpdateCallback(BillboardVideoEffect* ve) 
		{
			_ve = ve ;
		}
		~BillboardVideoUpdateCallback() {} ;
		virtual void operator()( osg::Node* node, osg::NodeVisitor* nv)
		{
			if(_ve)
				_ve->callback();
		}
	private:
		BillboardVideoEffect*				_ve;
	} ;
}